package com.machinecontroller.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.Toast;

import com.machinecontroller.StrongService;
import com.machinecontroller.constant.McConfig;
import com.machinecontroller.ui.MainActivity;
import com.machinecontroller.utils.AppUtil;
import com.machinecontroller.utils.Logger;
import com.machinecontroller.utils.ServiceUitl;
import com.machinecontroller.utils.SilentInstall;
import com.machinecontroller.utils.SpUtil;
import com.machinecontroller.utils.ToastUtil;
import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.beta.download.DownloadListener;
import com.tencent.bugly.beta.download.DownloadTask;

import java.io.File;
import java.io.IOException;


/**
 * http://blog.csdn.net/liuzg1220
 *
 * @author henry
 */
public class UpdateService extends Service {


    private static final String TAG = "UpdateSerivce";

    public UpdateService() {
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //自动升级
        Logger.e("aaa", "UpdateService");
        new Thread(() -> {
            int tryTimes = 0;//重试次数
            Logger.e("aaa", "start");
            while (tryTimes < 3) {
                if (!TextUtils.isEmpty(SpUtil.getAppPath(getApplicationContext()))) {
                    Logger.e("aaa", SpUtil.getAppPath(getApplicationContext()));
                    if (AppUtil.installAppSilent(SpUtil.getAppPath(getApplicationContext()))) {
                        Logger.e("aaa", "安装成功");
                        File file = new File(SpUtil.getAppPath(getApplicationContext()));
                        if (file.exists()) {
                            file.delete();
                            SpUtil.putAppPath(getApplicationContext(), "");
                        }
                    } else {
                        Logger.e("aaa", "安装失败");
                    }
//                    installApk();
//                    if (!AppUtil.isAppRoot()) {
//                        Logger.e("aaa", "安装：" + AppUtil.execRootCmdSlient("run_bktools.sh"));
//                    } else {
//                        Logger.e("aaa", "安装：" + SilentInstall.install(SpUtil.getAppPath(getApplicationContext())));
//                    }
                    tryTimes = 4;
                } else {
                    tryTimes++;
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            Logger.e("aaa", "end");
        }).start();
    }

    private void installApk() {
        try {
            Process install = Runtime.getRuntime().exec(new String[]{"su", "-c", "pm install -r " + SpUtil.getAppPath(getApplicationContext())});
            Process install2 = Runtime.getRuntime().exec(new String[]{"su", "-c", "am start -a com.machinecontroller"});
//	        adb shell am start -n
//			Process install = Runtime.getRuntime().exec(new String[] {"pm install -r /mnt/sdcard/CQYTJ.apk"});
            install.waitFor();
            install2.waitFor();
            if (install.exitValue() == 0) {
                // Success 安装成功
                Intent mintent = this.getPackageManager().getLaunchIntentForPackage("com.machinecontroller");
//	        	Intent mintent = getPackageManager().getLaunchIntentForPackage("com.example.phonemapoffline");
                startActivity(mintent);
            } else {
                // Fail
            }
        } catch (IOException e) {
            System.out.println(e.toString());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
